package com.scut.edu.wanyou.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class UserKeeper {
	
	private static final String PREFERENCES_NAME = "com_scut_edu_wy";
	
	/**
	 * 保存accesstoken到SharedPreferences
	 * @param context Activity 上下文环境
	 * @param token Oauth2AccessToken
	 */
	public static void keepUserToken(Context context, UserToken token) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.putInt("userid", token.getUserid());
		editor.putString("username", token.getUsername());
		editor.putString("password", token.getPassword());
		editor.commit();
	}
	
	public static void clear(Context context){
	    SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
	    Editor editor = pref.edit();
	    editor.clear();
	    editor.commit();
	}

	public static UserToken readUserToken(Context context){
		UserToken token = new UserToken();
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
		token.setUserid(pref.getInt("userid", -1));
		token.setUsername(pref.getString("username", ""));
		token.setPassword(pref.getString("password", ""));
		return token;
	}

}
